package com.study.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.study.model.mdao.TousuDAO;
import com.study.services.TousuService;

@Controller
public class TousuController {
	@Autowired
	TousuService service;
	
	@GetMapping("/tousutys")
	public ModelAndView getForm5() {
		ModelAndView mv = new ModelAndView("afterSale/kehutousu");
		mv.addObject("tousucalzz", service.selectByclazz());
		mv.addObject("tousuresult", service.selectByResult());
		return mv;
	}

	/**
	 * 查看服务（第一次进入）
	 */
	@RequestMapping("/tousuty")
	@ResponseBody
	public String service1(HttpSession session) {
		Integer cusid = (Integer) session.getAttribute("cusview");
		List<JSONObject> list = service.selectBySel(1, 6, "", "",cusid);
		//pagerhelper自定的PageInfo类.会把ArrayList的信息，整合到PageInfo对象中的list属性中。JSON亦然
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
		String json = JSONObject.toJSONString(pager);
		//System.out.println(json);
		session.removeAttribute("cusview");
		return json;
	}
	
	@GetMapping("tousuty2")
	public ModelAndView getForm2() {
		ModelAndView mv = new ModelAndView("/afterSale/addlodge");
		mv.addObject("tousu",new JSONObject());
		return mv;
	}
	/**
	 * 获客采集分页+全部+带条件查询 ty
	 * 
	 * @return
	 */
	@PostMapping("afterSale/kehutousu")
	@ResponseBody
	public String getForm3(String input, String selects, int currentPage, int pagesize) {
		List<JSONObject> list = service.selectBySel(currentPage, pagesize, input.trim(), selects.trim(),0);
		
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
        String json = JSONObject.toJSONString(pager);
		return json;
	}
	
	/**
	 * 添加
	 * @param theme
	 * @param user
	 * @param type
	 * @param date1
	 * @param date2
	 * @param custname
	 * @param degree
	 * @param progress
	 * @param aresult
	 * @param spendtime
	 * @param feedback
	 * @param linkman
	 * @param more
	 * @param session
	 * @return
	 */
	@PostMapping("tousuform")
	@ResponseBody
	public boolean addForm(String theme, String user, String type, 
			 Date date1,Integer custid,String degree,
			String progress, String aresult,
			String feedback,String linid,HttpSession session) {
		//System.out.println(user+"编辑投诉"+type+"type"+degree+"degree"+aresult+"aresult"+spendtime+"spendtime");
		
		Integer useid = (Integer) session.getAttribute("useid");
		boolean result = service.addTousu(theme,useid,type,date1,custid,degree,progress,aresult,feedback,linid);
		System.out.println(result+"我是售后投诉的结果");
		return result;
	}
	
	/**
	 * 删除投诉
	 * @param goo_id
	 * @return
	 */
	@PostMapping("/delTousu")
	@ResponseBody
	public String delete(Integer c_id) {
		System.out.println(c_id);
		service.delete(c_id);
		return "ok";
	}
	
	/**
	 *编辑
	 * @param theme
	 * @param user
	 * @param type
	 * @param dates
	 * @param times
	 * @param custname
	 * @param degree
	 * @param progress
	 * @param aresult
	 * @param spendtime
	 * @param feedback
	 * @param linkman
	 * @param more
	 * @return
	 */
	@PostMapping("editertousuform")
	@ResponseBody
	public boolean editorForm(String theme, String type, 
			 Date date1 , Integer custid,String degree,
			String progress, String aresult,
			String feedback,Integer linid,String more,String id,HttpSession session) {
		//System.out.println(id+"编辑");
		Integer useid = (Integer) session.getAttribute("useid");
		boolean result = service.editorService(theme,useid,type,date1,custid,degree,progress,aresult,feedback,linid,more,id);
		System.out.println(result+"我是售后投诉的结果");
		return result;
	}
	
	
	/**
	 * 编辑
	 */
	@GetMapping("tousuEditor")
	public ModelAndView addCustomerByColle(HttpServletRequest request) {
		String parameter = request.getParameter("id");
		JSONObject tousu = service.selectBycid(parameter);
		ModelAndView mv = new ModelAndView("/afterSale/addlodge");
		mv.addObject("tousu", tousu);
		return mv;
	}
}
